package com.yang.mall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.mysql.cj.util.StringUtils;
import com.yang.mall.product.entity.CategoryBrandRelationEntity;
import com.yang.mall.product.service.CategoryBrandRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yang.common.utils.PageUtils;
import com.yang.common.utils.Query;

import com.yang.mall.product.dao.BrandDao;
import com.yang.mall.product.entity.BrandEntity;
import com.yang.mall.product.service.BrandService;


@Service("brandService")
public class BrandServiceImpl extends ServiceImpl<BrandDao, BrandEntity> implements BrandService {
    @Autowired
    CategoryBrandRelationService categoryBrandRelationService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String key = (String)params.get("key");
        QueryWrapper<BrandEntity> queryWrapper = new QueryWrapper<>();
        if(!StringUtils.isNullOrEmpty(key)){
            queryWrapper.eq("brand_id",key).or().like("name",key);
        }
        IPage<BrandEntity> page = this.page(
                new Query<BrandEntity>().getPage(params),
                queryWrapper
        );

        return new PageUtils(page);
    }

    @Override
    public void removeBrandByIds(List<Long> asList) {
        //逻辑删除
        baseMapper.deleteBatchIds(asList);
    }

    @Override
    public void updateDetail(BrandEntity brand) {
        //保证冗余字段的数据一致
        UpdateWrapper<BrandEntity> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("name",brand.getName())
                .set("logo",brand.getLogo())
                .set("descript",brand.getDescript())
                .set("show_status",brand.getShowStatus())
                .set("first_letter",brand.getFirstLetter())
                .set("sort",brand.getSort())
                .eq("brand_id",brand.getBrandId());
        this.update(updateWrapper);
        if(!StringUtils.isNullOrEmpty(brand.getName())){
            //同步其他关联表中的数据
            categoryBrandRelationService.updateBrand(brand.getBrandId(),brand.getName());
            //TODO 更新其他关联
        }
    }

}